package com.xieteng.service;

import com.xieteng.dao.UserDAO;
import com.xieteng.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDAO userDAO;

    @Override
    public void save(User user) {
        User userByUserName = userDAO.findByUserName(user.getUsername());
        if (userByUserName == null) {
            user.setStatus("已激活");
            user.setRegisterTime(new Date());
            userDAO.save(user);
        } else {
            throw new RuntimeException("用户名已存在");
        }
    }

    @Override
    public User login(User user) {
        User userByUserName = userDAO.findByUserName(user.getUsername());
        if (userByUserName != null) {
            if (user.getPassword().equals(userByUserName.getPassword())) {
                return userByUserName;
            } else {
                throw new RuntimeException("密码输入错误");
            }
        } else {
            throw new RuntimeException("用户名不存在");
        }
    }
}
